﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

using Shove.Database;

public partial class page_Backup : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnOK_2.Enabled = isMasterSiteOnly;
        //Response.Redirect("Welcome.aspx");
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;

        RequestLoginPage = this.Request.Url.AbsoluteUri;

//       RequestCompetences = Competences.BuildCompetencesList(Competences.Options);

        isMasterSiteOnly = true;

        base.OnInit(e);
    }

    #endregion

    protected void btnOK_Click(object sender, EventArgs e)
    {
        byte[] Result = MSSQL.BackupDataToZipStream();

        if (Result == null)
        {
            Shove._Web.JavaScript.Alert(this.Page, "数据表备份失败！");

            return;
        }

        HttpResponse response;

        response = Page.Response;
        response.AppendHeader("Content-Disposition", "attachment;filename=ShoveSFS_DataBackup_" + System.DateTime.Now.ToString().Replace("-", "_").Replace(" ", "_").Replace(":", "_") + ".zip");
        Response.ContentType = "application/octet-stream";
        Response.BinaryWrite(Result);
        Response.Flush();
        response.End();
    }

    protected void btnOK_2_Click(object sender, EventArgs e)
    {
        SqlConnection conn = Shove.Database.MSSQL.CreateDataConnection();
        if (conn == null)
        {
            Shove._Web.JavaScript.Alert(this.Page, "数据库备份失败！");
            return;
        }

        string DatabaseName = this.Server.MapPath("../App_Data/" + conn.Database + ".bak");
        string ShortName = conn.Database + ".zip";

        conn.Close();

        if (Shove.Database.MSSQL.BackupDatabase(DatabaseName, true, true) < 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "数据库备份失败！");

            return;
        }

        HttpResponse response;

        response = Page.Response;
        response.AppendHeader("Content-Disposition", "attachment;filename=" + ShortName);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(DatabaseName + ".zip");
        Response.Flush();
        response.End();
    }
}